//
//  JMReNameAuthTVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/5/4.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMReNameAuthTVController.h"
#import "JMCellView.h"
@interface JMReNameAuthTVController ()<UITextFieldDelegate>

@end

@implementation JMReNameAuthTVController
{
    UITextField* _namefield;
    UITextField* _identityCardNumfield;
    UITextField* _bankNamefield;
    UITextField* _bankNumfield;
    UIButton *_chooseBankBut;
    
    UITableView *chooseBankNameTabelVc;
    UILabel* remindlabel;
    UIButton *okbut;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"实名开户";
    
    [self setupOther];
    
    [self addRemindLabel];
    
    [self addOkBut];
    
}
-(void)setupOther{
    
    self.tableView.scrollEnabled = NO;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    self.tableView.tableHeaderView = view;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BACKCOLOR;
    UITapGestureRecognizer *singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1;
    [self.tableView addGestureRecognizer:singleRecognizer];
}
- (void)handleSingleTapFrom
{
    [_namefield resignFirstResponder];
    [_identityCardNumfield resignFirstResponder];
    [_bankNamefield resignFirstResponder];
    [_bankNumfield resignFirstResponder];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"AuthenticationCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //添加cell里面的控件
    UILabel *label = [ARC createLabelWithFrame:CGRectMake(10, 0, 100, CELLHIGHT) Font:13 Text:@""];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label];
    // 分隔线
    UIView *lineView = [ARC createViewWithFrame:CGRectMake(10, CELLHIGHT-0.5, SCREEN_WIDTH-10, 0.5)];
    lineView.backgroundColor = BACKCOLOR;
    [cell.contentView  addSubview:lineView];
    
    
    if (indexPath.row == 0) {
        
        label.text=@"开户人姓名";
        
        [cell.contentView addSubview:label];
        
        if (!_namefield) {
            
            _namefield = [self addNameFieldWithFrame:CGRectMake(label.frame.size.width, 0, SCREEN_WIDTH-label.frame.size.width, CELLHIGHT)];
            
            [cell.contentView  addSubview:_namefield];
        }
    }else if(indexPath.row == 1){
        
        label.text=@"身份证号码";
        
        if (!_identityCardNumfield) {
            
            _identityCardNumfield = [self addIdentityCardNumFieldWithFrame:CGRectMake(label.frame.size.width, 0, SCREEN_WIDTH-label.frame.size.width, CELLHIGHT)];
            
            [cell.contentView  addSubview:_identityCardNumfield];
        }
    }else if (indexPath.row == 2){
        
        label.text=@"选择所属银行";
        
        if (!_bankNamefield) {
            CGFloat _bankNamefieldH = 30;
            _bankNamefield = [self addBackNameFieldWithFrame:CGRectMake(label.frame.size.width, (CELLHIGHT-_bankNamefieldH+6)/2, SCREEN_WIDTH-label.frame.size.width-20, _bankNamefieldH)];
            /**
             *  选择银行按钮
             */
            CGFloat chooseBankButW = 20;
            CGFloat chooseBankButH = 20;
            //            UIButton *chooseBankBut = [ARC createButtonWithFrame:CGRectMake(_bankNamefield.frame.size.width-chooseBankButW, (_bankNamefield.frame.size.height-chooseBankButH)*0.5, 20, chooseBankButH) Target:self Sel:@selector(chooseBankClicked:) Title:@"选择银行" NormalImageName:@"Arrow-副本" SelectImageName:@"Arrow-副本"];
            
            UIButton *chooseBankBut = [ARC createButtonWithFrame:CGRectMake(_bankNamefield.frame.size.width-chooseBankButW, (_bankNamefield.frame.size.height-chooseBankButH)*0.5, 20, chooseBankButH) Target:self Sel:@selector(chooseBankClicked:) Title:@"" NormalImageName:@"Arrow-副本"];
            [chooseBankBut setTitle:@"" forState:UIControlStateSelected];
            //            checkBankBut.backgroundColor = [UIColor redColor];
            _chooseBankBut =chooseBankBut;
            
            [_bankNamefield addSubview:chooseBankBut];
            
            [cell.contentView  addSubview:_bankNamefield];
        }
    }else if (indexPath.row == 3){
        
        label.text=@"银行卡号";
        if (!_bankNumfield) {
            
            _bankNumfield = [self addbankNumFieldWithFrame:CGRectMake(label.frame.size.width, 0, SCREEN_WIDTH-label.frame.size.width, CELLHIGHT)];
            [cell.contentView  addSubview:_bankNumfield];
        }
        
    }
    
    
    return [JMCellView setupCell:cell HideLine:YES];
}

- (JMTextField *)addNameFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:FONTSIZE
                                                       Placeholder:@"请输入您的真实姓名"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.keyboardType =  UIKeyboardTypeDefault;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
    return  textField;
    
}

- (JMTextField *)addIdentityCardNumFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:FONTSIZE
                                                       Placeholder:@"请输入您的身份证号码"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.keyboardType =  UIKeyboardTypePhonePad;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
    return  textField;
    
}
- (JMTextField *)addBackNameFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:FONTSIZE
                                                       Placeholder:@"请选择所属银行"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.keyboardType =  UIKeyboardTypeDefault;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
    return  textField;
    
}

- (JMTextField *)addbankNumFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:FONTSIZE
                                                       Placeholder:@"请输入银行卡号"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.keyboardType =  UIKeyboardTypePhonePad;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
    return  textField;
    
}
-(void)addRemindLabel{
    CGFloat viewX = 10;
    CGFloat ViewW = 320;
    CGFloat viewH = 60;
    int labelFont = 12;
    UIView *labelView = [ARC createViewWithFrame:CGRectMake(0, 210, ViewW, viewH)];
    
    UILabel *label1 = [ARC createLabelWithFrame:CGRectMake(10, 0,ViewW, 20) Font:labelFont Text:@"温馨提示:"];
    label1.textColor = RGB(150, 150, 150);
    label1.textAlignment = NSTextAlignmentLeft;
    UILabel *label2 = [ARC createLabelWithFrame:CGRectMake(viewX, 20, ViewW, 20) Font:labelFont Text:@"1，银行卡绑定必须先实名认证。"];
    label2.textColor = RGB(150, 150, 150);
    label2.textAlignment = NSTextAlignmentLeft;
    UILabel *label3 = [ARC createLabelWithFrame:CGRectMake(10, 40, ViewW, 20) Font:labelFont Text:@"2，银行卡的开户人必须与实名认证一致。"];
    label3.textColor = RGB(150, 151, 152);
    label3.textAlignment = NSTextAlignmentLeft;
    
    [labelView addSubview:label1];
    [labelView addSubview:label2];
    [labelView addSubview:label3];
    [self.view addSubview:labelView];
}
-(void)addOkBut{
    
    CGFloat btnX = 300;
    CGFloat btnH = 40;
    
    okbut = [ARC createButtonWithFrame:CGRectMake(30,btnX, 260, btnH)
                                Target:self
                                   Sel:@selector(okBtnClick:)
                                 Title:@"确定"];
    
    [okbut setBackgroundImage:[UIImage imageNamed:@"logoin_btn"] forState:UIControlStateNormal];
    okbut.frame = [ToolViewAndData MyAutoLayout:okbut];
    [okbut.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    okbut.tintColor=[UIColor whiteColor];
    [self.view addSubview:okbut];
    
}
/**
 * 点击选择银行卡
 */
-(void)chooseBankClicked:(UIButton *)sender{
    //
    //    UITableView *chooseBankTabelVc = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-120, 150, 100, 132) style:UITableViewStylePlain];
    //    chooseBankNameTabelVc = chooseBankTabelVc;
    //    chooseBankTabelVc.backgroundColor = [UIColor blueColor];
    //    [self.view addSubview:chooseBankTabelVc];
    //    JMStrategyPeoperViewController *strategyPeoperVc = [[JMStrategyPeoperViewController alloc] init];
    //    [self.navigationController pushViewController:strategyPeoperVc animated:YES];
    
}
/**
 * 点击进行实名开户
 */
-(void)okBtnClick:(UIButton*)btn

{
    
    if ([_namefield.text isEqual:@""]) {
        SHOWALERT(@"姓名不能为空");
        return;
    }
    if (_identityCardNumfield.text&&![ToolViewAndData isValidateIdentityCard:_identityCardNumfield.text]) {
        SHOWALERT(@"请输入正确的身份证号码");
        return;
    }
    //    if (_bankNumfield.text&&![ToolViewAndData isBankCardNo:_bankNumfield.text]) {
    //        SHOWALERT(@"请输入正确的银行卡号码");
    //        return;
    //    }
}
@end
